<?php
declare (strict_types = 1);

namespace app\xapply\controller;

use think\facade\Db;
use think\facade\Config;
use think\facade\Cache;

class Install
{
    /**
     * 接口安装
     */
    public function index()
    {
        $prefix = Config::get('database.connections.mysql.prefix');
        $sql = app_path().'install/install.sql';
        $lock = app_path().'install/install.lock';
        if (file_exists($lock)){
            return '已安装，如需重新安装请删除'.$lock;
        }
        $content = file_get_contents($sql);
        $arr = explode(";", $content);
        Db::startTrans();
        try {
            // 导入sql
            foreach ($arr as $value){
                $value = trim($value);
                if ($value){
                    $value = str_replace('__PREFIX__', $prefix, $value);
                    Db::execute($value);
                }
            }
            // 新增功能模块
            Db::name('AdminModule')->insert([
                'title' => 'Xapply模块',
                'name' => 'xapply',
                'desc' => '勾股OA连接Xapply app模块',
                'type' => 3,
                'sourse' => 2,
                'status' => 1,
                'create_time' => time(),
                'update_time' => time(),
            ]);
            // 新增功能节点
            $nodes = [
                'pid' => '',
                'src' => '',
                'title' => 'Xapply管理',
                'name' => 'Xapply管理',
                'module' => 'xapply',
                'icon' => 'icon-app',
                'menu' => 1,
                'sort' => 6,
                'status' => 1,
                'create_time' => time(),
                'update_time' => time(),
                'next' => [
                    [
                        'pid' => '',
                        'src' => 'xapply/myapp/index',
                        'title' => '应用管理',
                        'name' => '应用管理',
                        'module' => 'xapply',
                        'icon' => '',
                        'menu' => 1,
                        'sort' => 1,
                        'status' => 1,
                        'create_time' => time(),
                        'update_time' => time(),
                        'next' => [
                            [
                                'pid' => '',
                                'src' => 'xapply/myapp/add',
                                'title' => '新增/编辑',
                                'name' => '应用管理',
                                'module' => 'xapply',
                                'icon' => '',
                                'menu' => 2,
                                'sort' => 0,
                                'status' => 1,
                                'create_time' => time(),
                                'update_time' => time(),
                                'next' => []
                            ],
                            [
                                'pid' => '',
                                'src' => 'xapply/myapp/delete',
                                'title' => '删除',
                                'name' => '应用管理',
                                'module' => 'xapply',
                                'icon' => '',
                                'menu' => 2,
                                'sort' => 0,
                                'status' => 1,
                                'create_time' => time(),
                                'update_time' => time(),
                                'next' => []
                            ],
                            [
                                'pid' => '',
                                'src' => 'xapply/myapp/perm',
                                'title' => '权限设置',
                                'name' => '应用管理',
                                'module' => 'xapply',
                                'icon' => '',
                                'menu' => 2,
                                'sort' => 0,
                                'status' => 1,
                                'create_time' => time(),
                                'update_time' => time(),
                                'next' => []
                            ],
                            [
                                'pid' => '',
                                'src' => 'xapply/myapp/select',
                                'title' => '选择',
                                'name' => '应用管理',
                                'module' => 'xapply',
                                'icon' => '',
                                'menu' => 2,
                                'sort' => 0,
                                'status' => 1,
                                'create_time' => time(),
                                'update_time' => time(),
                                'next' => []
                            ],
                        ]
                    ],
                    [
                        'pid' => '',
                        'src' => 'xapply/group/index',
                        'title' => '分组管理',
                        'name' => '分组管理',
                        'module' => 'xapply',
                        'icon' => '',
                        'menu' => 1,
                        'sort' => 2,
                        'status' => 1,
                        'create_time' => time(),
                        'update_time' => time(),
                        'next' => [
                            [
                                'pid' => '',
                                'src' => 'xapply/group/add',
                                'title' => '新建/编辑',
                                'name' => '分组管理',
                                'module' => 'xapply',
                                'icon' => '',
                                'menu' => 2,
                                'sort' => 0,
                                'status' => 1,
                                'create_time' => time(),
                                'update_time' => time(),
                                'next' => []
                            ],
                            [
                                'pid' => '',
                                'src' => 'xapply/group/delete',
                                'title' => '删除',
                                'name' => '分组管理',
                                'module' => 'xapply',
                                'icon' => '',
                                'menu' => 2,
                                'sort' => 0,
                                'status' => 1,
                                'create_time' => time(),
                                'update_time' => time(),
                                'next' => []
                            ],
                            [
                                'pid' => '',
                                'src' => 'xapply/group/select',
                                'title' => '选择',
                                'name' => '分组管理',
                                'module' => 'xapply',
                                'icon' => '',
                                'menu' => 2,
                                'sort' => 0,
                                'status' => 1,
                                'create_time' => time(),
                                'update_time' => time(),
                                'next' => []
                            ],
                        ]
                    ],
                ]
            ];
            $this->nodes($nodes);
            // 导入配置
            Db::name('config')->insert([
                'title' => '移动推送',
                'name' => 'xapply',
                'create_time' => time(),
                'update_time' => time(),
            ]);
            $config_view_file = root_path().'app/home/view/conf/xapply.html';
            $source_config_view_file = app_path().'install/app/home/view/conf/xapply.html';
            copy($source_config_view_file, $config_view_file);
            $pub = public_path().'static/xapply/';
            $wgt_file = $pub.'__UNI__5FDB086.wgt';
            $wgt_source_file = app_path().'install/public/static/xapply/__UNI__5FDB086.wgt';
            if (!is_dir($pub)){
                mkdir($pub);
            }
            copy($wgt_source_file, $wgt_file);
            file_put_contents($lock, time());
            Cache::clear();
            // 提交事务
            Db::commit();
            $message = '安装成功';
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $message = '安装失败: '.$e->getMessage();
        }
        return $message;
    }

    private function nodes($value, $pid = 0){
        $next = $value['next'];
        unset($value['next']);
        $value['pid'] = $pid;
        $node_id = Db::name('AdminRule')->insertGetId($value);
        $group = Db::name('AdminGroup')->find(1);
        if (!empty($group)) {
            $newGroup['id'] = 1;
            $newGroup['rules'] = $group['rules'] . ',' . $node_id;
            Db::name('AdminGroup')->strict(false)->field(true)->update($newGroup);
        }
        if ($next){
            foreach ($next as $item){
                $this->nodes($item, $node_id);
            }
        }
    }
}
